<template>
    <div class="div-background" >
        <div
            style="width: 400px;height: 350px;margin: 150px auto;background-color: rgba(107, 149, 224, 0.5);border-radius: 10px;">

            <div style="width: 100%;height: 100px;font-size: 30px;line-height: 100px;text-align: center;color: #4a5ed0;">
                欢迎登录
            </div>
            <div style="margin-top: 25px;text-align: center;height: 320px;">
                <el-form :model="loginInfo" ref="loginForm" :rules="rules" @keyup.enter.native="loginUser">
                    <el-form-item prop="name">
                        <el-input v-model="loginInfo.name" prefix-icon="el-icon-user" style="width: 80%;"
                            placeholder="请输入用户名"></el-input>
                    </el-form-item>
                    <el-form-item prop="password">
                        <el-input v-model="loginInfo.password" prefix-icon="el-icon-lock" style="width: 80%;"
                            placeholder="请输入密码" show-password></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button style="width: 80%;margin-top: 10px;" type="primary" @click="loginUser">登录</el-button>
                    </el-form-item>
                </el-form>
            </div>
        </div>
    </div>
</template>
<script>
import { login } from '../api/userApi'
export default {
    name: 'LoginView',
    props: {

    },
    data() {
        return {
            rules: {
                name: [
                    { required: true, message: '用户名不能为空', trigger: 'blur' }
                ],
                password: [
                    { required: true, message: '密码不能为空', trigger: 'blur' }
                ],
            },
            //登录信息
            loginInfo: {
                name: '',
                password: ''
            }
        }
    },
    methods: {
        //用户登录
        loginUser() {
            this.$refs.loginForm.validate((valid) => {
                if (valid) {
                    login(this.loginInfo).then((response) => {
                        if (response.code === '0') {
                            this.$message.success("登录成功")
                            localStorage.setItem("user", JSON.stringify(response.data))
                            this.$router.push("/")
                        } else {
                            this.$message.error(response.msg)
                        }
                    })
                } else {
                    return false
                }
            })

        }
    },
    created() {

    },
    watch: {

    }
}
</script>
<style scoped>
/* 解决前端校验提示不在输入框下方的问题 */
/deep/ .el-form-item__error {
    padding-left: 40px;

}
.div-background{
    background-image: url('../assets/loginBackground1.jpg');
    background-size: cover;
    width: 100%;
    height: 850px;
    position: absolute;
}
</style>